package com.example.housingRentalSystem.utils;

import lombok.Data;

import java.util.Collections;
import java.util.List;

/**
 * @author 王硕
 * @version jdk21
 * @className PageResponse
 * @description
 * @date 2025/3/21 17:32
 */
@Data
public class PageResponse<T> {
    // 当前页码
    private int page;
    // 每页数量
    private int size;
    // 总记录数
    private long total;
    // 总页数
    private int totalPages;
    // 数据列表
    private List<T> data;

    /**
     * 通过 MyBatis Page 对象构建分页响应
     */
    public static <T> PageResponse<T> build(com.github.pagehelper.Page<T> page) {
        PageResponse<T> response = new PageResponse<>();
        response.setPage(page.getPageNum());
        response.setSize(page.getPageSize());
        response.setTotal(page.getTotal());
        response.setTotalPages(page.getPages());
        response.setData(page.getResult());
        return response;
    }
    public static <T> PageResponse<T> empty() {
        PageResponse<T> response = new PageResponse<>();
        response.setPage(1);
        response.setSize(10);
        response.setTotal(0L);
        response.setTotalPages(0);
        response.setData(Collections.emptyList());
        return response;
    }

}
